﻿#ifndef PIXEL2MECH_H
#define PIXEL2MECH_H

#include "Matrix/Matrix.h"
#include "scvision_global.h"
#include <QMatrix>
#include <QPointF>

struct SCVISIONSHARED_EXPORT PrOffset
{
public:
    PrOffset();

    PrOffset(double x, double y, double theta);

    void ReSet();

    QString toString() const;

    QString toShortString() const;

    double X;
    double Y;
    double Theta;
    double W;
    double H;
    double O_X;
    double O_Y;
};

PrOffset SCVISIONSHARED_EXPORT operator-(const PrOffset &left, const PrOffset &right);
PrOffset SCVISIONSHARED_EXPORT operator*(const PrOffset &offset, double multiplier);

class Pixel2Mech
{
public:
    Pixel2Mech();

    //用以前校正过的数据来初始化
    Pixel2Mech(const QMatrix &matrix, const QPointF &center_of_pixel);

    void ChangeParameter(const QMatrix &matrix, const QPointF &center_of_pixel);

    //三点校正，提供三个像素点，和对应的三个机械点，还有相机的中心点的坐标，返回变换矩阵供存档
    QMatrix DoCalibration(const QPointF pixel_points[3], const QPointF mech_points[3], const QPointF &center_of_pixel);

    //计算传入的像素点和相机中心的机械距离
    bool CalcMechDistance(const QPointF &pixel_point, QPointF &mech_point);

    QPointF pixel2MechPoint(const QPointF &pixelPoint);

    QPointF getMechCenter() const;

    bool hasCalibration();

    void setHasCalibration(bool value);

    double getXResolution();

    double getYResoultion();

private:
    //是否经过校正
    bool has_calibration = false;
    //变换矩阵
    QMatrix transformation;
    //相机中心坐标
    QPointF center_of_pixel;
    //机械中心坐标
    QPointF center_of_mech;
};

//求仿射转换的矩阵
CMatrix getAffineTransform(const QPointF src[], const QPointF dst[]);

#endif    // PIXEL2MECH_H
